package ConcorrenciaOptimista;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

import JDBC.Constants;
import JDBC.DBConnection;
import JDBC.DBConnection.ConnectionHandler;


public class EventoPessimista {
	
	private int idEvento;
	private String nomeEvento;
	private Date data;
	private Time hora;
	private List<String> bilhetes;
	
	private EventoPessimista(int idEvento, String nomeEvento, Date data, Time hora) {
		this.idEvento = idEvento;
		this.nomeEvento = nomeEvento;
		this.data = data;
		this.hora = hora;
	}

	public int getIdEvento() {
		return idEvento;
	}

	public String getNomeEvento() {
		return nomeEvento;
	}

	public String getDataString() {
		return data == null ? "Sem data agendada" : data.toGMTString();
	}

	public String getHoraString() {
		return hora == null ? "Sem hora agendada" : hora.toGMTString();
	}

	public List<String> getBilhetes() {
		return bilhetes;
	}

	public void setBilhetes(List<String> bilhetes) {
		this.bilhetes = bilhetes;
	}
	
			
	public static List<EventoPessimista> obterEventos() {
		

		String query = "select idEvento as id, nomeEvento as nome, data, hora from eventoOferecido;";
		DBConnection dbconn = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);


		JDBC.DBConnection.ConnectionHandler ch = dbconn.getConnectionHandlerInstance();

		dbconn.returnResultSetSelectStatement(query, 1, ch);
		
        
        List<EventoPessimista> eventos = new ArrayList<EventoPessimista>();
        try {
            while(ch.r.next()) {
            	EventoPessimista evento = new EventoPessimista(ch.r.getInt("id"), ch.r.getString("nome"), ch.r.getDate("data"), ch.r.getTime("hora"));
                eventos.add(evento);
            }
            ch.r.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        dbconn.setCommit(ch);
        dbconn.closeDatabase();
    	return eventos;
	}

	public static List<String> obterLugaresEvento(int idEvento) {
		
		DBConnection dbConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);

		String query ="select * from Bilhetes";
        JDBC.DBConnection.ConnectionHandler ch = dbConnection.getConnectionHandlerInstance();
     
        dbConnection.returnResultSetSelectStatement(query, 0, ch);
        
        List<String> lugares = new ArrayList<String>();
        try {
            while(ch.r.next()) {
            	if(ch.r.getInt("idEvento") == idEvento) {
            		if(ch.r.getInt("estado") == 0) {
            			lugares.add(ch.r.getString("numeroLugar"));
            		}
            	}
            }
            ch.r.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //dbConnection.setCommit(ch);
        dbConnection.closeDatabase();
 
    	return lugares;
			
	}
	


	public static ConnectionHandler bloquearLugar(DBConnection dbconn, Integer idEvento, String numeroLugar) {
		//String query ="select estado from Bilhetes with (xlock) where idEvento = '"+idEvento+"' and numeroLugar = '"+numeroLugar+"'";
//		String query ="SELECT numeroLugar, estado FROM (SELECT * FROM Bilhetes where idEvento = '"+idEvento+"'and numeroLugar = '"+numeroLugar+"')  FOR UPDATE OF numeroLugar, estado";
		String query = "select * from bilhetes for update";
		
		ConnectionHandler ch = dbconn.getConnectionHandlerInstance();
		//dbconn.returnResultSetSelectStatement("select * from bilhetes", 1, ch);
        dbconn.setIsolationLevel("3", ch);

        dbconn.returnResultSetSelectStatement(query, 0, ch);

        //dbconn.setCommit(ch);
       return ch;
    }
	
public static void reservarLugarEvento(int idEvento, String lugar, DBConnection dbconn, ConnectionHandler ch) {
		
		String query ="update  Bilhetes set estado=1  where idEvento = "+idEvento+" and numeroLugar='"+lugar+"'";
		dbconn.updateStatement(query, ch);
       
		dbconn.setCommit(ch); 
		dbconn.closeDatabase();    		           
               
	}

	public static void anularTransacao(DBConnection db) {
		db.rollback();
		db.closeDatabase();
	}



}
